package com.second.app.designpattern.observer.eventbus;

import com.google.common.eventbus.AsyncEventBus;
import com.second.app.designpattern.observer.eventbus.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.Executors;

/**
 * @Author GYZ
 * @Date 2023/11/10
 * <pre>
 *
 * </pre>
 */
@RestController
public class UserController {

    @Autowired
    IUserService iUserService;


    @RequestMapping("user/register")
    public Long register(String telephone,String password){
        Long userId = iUserService.registerUser(telephone, password);
        AsyncEventBus eventBus = new AsyncEventBus(Executors.newFixedThreadPool(20));

        eventBus.register(new RegPromotionObserver(new IPromotionService() {
            @Override
            public void issueNewUserExperienceCash(Long userId) {
                System.out.println("给用户："+userId+"发送了经验券");
            }
        }));
        eventBus.register(new RegNotificationObserver(new INotificationService() {
            @Override
            public void sendInboxMessage(Long useId, String message) {
                System.out.println("给用户："+userId+"发送了消息，消息内容："+message);
            }
        }));
        eventBus.post(userId);
        System.out.println("注册用户成功");
        return userId;
    }
}
